import {
    addFav,
    removeFav,
    like,
    unlike,
    commentUnlike,
    commentLike
    
} from '@/api/api';
export default {
    // 收藏于取消二合一
    toggleFav() {
        //   点击时先判断登陆没


        // 没登陆跳去登录

        // 登陆的话判断原来收藏了没

        // 没收藏的话就是收藏操作
        // 收藏的话就是取消收藏
        // console.log(1);
        if (!this.checkLogin()) return
        let {
            article: {
                is_fav
            },
            uid,
            article_id
        } = this

        if (is_fav) {
            // 原来是收藏的话就取消
            removeFav({
                user_id: uid,
                article_id
            }).then(res => {
                this.$toast(res.msg)
                console.log(res);
                if (res.code == 0) {
                    this.article.is_fav = false
                }
            })
        } else {
            // 没收藏的话就收藏
            addFav({
                user_id: uid,
                article_id
            }).then(res => {
                this.$toast(res.msg)
                console.log(res);
                if (res.code == 0) {
                    // 让他高亮
                    this.article.is_fav = true

                }
            })
        }
    },

    // 点赞 取消点赞
    toggleLike() {
        if (!this.checkLogin()) return;
        let {
          article: { is_like },
          uid,
          article_id,
        } = this;
    
        if (is_like) {
          //  取消
          unlike({
            user_id: uid,
            article_id,
          }).then((res) => {
            console.log(res);
            this.$toast(res.msg);
            if (res.code == 0) {
            
              this.article.is_like = false;
            }
          });
        } else {
          
    
          like({
            user_id: uid,
            article_id,
          }).then((res) => {
            console.log(res);
            this.$toast(res.msg);
            if (res.code == 0) {
              //  让他高亮
              this.article.is_like = true;
            }
          });
        }
      },

      // 评论点赞
      toggleCommentLike(comment_id,index,is_like) {
        
        if (!this.checkLogin()) return;
        let {
          uid,
        } = this;
    
        if (is_like) {
          //  取消
          commentUnlike({
            user_id: uid,
            comment_id,
          }).then((res) => {
            console.log(res);
            this.$toast(res.msg);
            if (res.code == 0) {
            
             this.commentList[index].is_like=false
             this.commentList[index].like_count=res.count
            }
          });
        } else {
          
    
          commentLike({
            user_id: uid,
            comment_id,
          }).then((res) => {
            console.log(res);
            this.$toast(res.msg);
            if (res.code == 0) {
              //  让他高亮
              this.commentList[index].is_like=true
              this.commentList[index].like_count=res.count
            }
          });
        }
      },
} 